package org.apache.servicecomb.samples.practise.houserush.customer.manage.aggregate;

import lombok.*;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: Snake Man
 * @system:
 * @DateTime: 2022/1/19 22:02
 * @Description:
 */
@Data
@Entity
@Table(name = "customers")
@SQLDelete(sql = "update customers set deleted_at = now() where id = ?")
@Where(clause = "deleted_at is null")
@EntityListeners(AuditingEntityListener.class)
public class Customer {

    @Id
    private Integer id;

    private String phone;

    private String name;

    private String idCard;

    private String realName;

    private String address;

    //orphanRemoval = true = 级联删除实体
    @OneToMany(mappedBy = "customer", cascade = {CascadeType.PERSIST, CascadeType.MERGE},
            orphanRemoval = true)
    private List<Qualification> qualifications = new ArrayList<>();

    @Temporal(TemporalType.TIMESTAMP)
    private Date deletedAt;

    @CreatedDate
    @Temporal(TemporalType.TIMESTAMP)
    private Date createdAt;

    @LastModifiedDate
    @Temporal(TemporalType.TIMESTAMP)
    private Date updatedAt;

}
